'use strict'

let $vue

function XcUtil(vue) { $vue = vue }

// =================================
// 扩展原生对象
// =================================
/**
 * 
 */
Date.prototype.dateFormat = function () {
  let d = this
  let month = d.getMonth() + 1
  let date = d.getDate()
  return d.getFullYear() + '-' + (month < 10 ? '0' : '') + month + '-' + (date < 10 ? '0' : '') + date
}

/**
 * @param millis 毫秒数
 */
XcUtil.prototype.to_mmssSSS = function (millis) {
  let date = new Date()
  date.setTime(millis)
  return date.getMinutes() + ':' + date.getSeconds() + '.' + date.getMilliseconds()
}

// =================================
// 其他业务工具
/**
 * @param status 节点状态
 */
XcUtil.prototype.nodeStatus = function (status) {
  status = status || 'OFF'
  switch (status) {
    case 'ON':
      return ['processing', 'status.on', status]
    case 'OFF':
    default:
      return ['default', 'status.off', status]
  }
}

let _loading

/**
 * 弹出loading
 */
XcUtil.prototype.showLoading = function (msg) {
  if (_loading) this.hideLoading()
  _loading = $vue.$Message.loading({
    content: msg || 'Loading...',
    duration: 0
  })
  setTimeout(this.hideLoading, 15000)
}

/**
 * 取消loading
 */
XcUtil.prototype.hideLoading = function () {
  try {
    _loading && _loading()
    _loading = null
  } catch (e) { }
}


export default XcUtil
